package com.neusoft.chenhanhua.gqing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

public class Pms_product_attribute_category30 {
	@Test
	public void insert() {
		
		Connection conn=null;
		String username="root";
		String passowrd="root";
		String driver="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8";
		try {
			//加载驱动到虚拟机
			Class.forName(driver);
			conn=DriverManager.getConnection(url,username,passowrd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//准备sql命令
		String sql="insert into Pms_product_attribute_category(id,name,attribute_count,param_count)values(110,2,3,4)";
		Statement st=null;
		try {
			st=conn.createStatement();
			//执行sql语句
			int row=st.executeUpdate(sql);
			System.out.println(row>0?"添加成功":"添加失败");
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (st!=null) {
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test 
	public void upate() {
		Connection conn=null;
		String username="root";
		String password="root";
		String driver="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/mall?useUnicode=true&charaterEncoding=utf-8";
		try {
			//加载驱动
			Class.forName(driver);
			conn=DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//准备sql命令
		String sql="update Pms_product_attribute_category set name=4 where id=1";
		Statement st=null;
		try {
			st=conn.createStatement();
			//执行数据库语句
			int row=st.executeUpdate(sql);
			System.out.println(row>0?"修改成功":"修改失败");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (st!=null) {
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
   @Test
   public void delete() {
	   Connection conn=null;
	   String username="root";
	   String password="root";
	   String driver="com.mysql.jdbc.Driver";
	   String url="jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8";
	   try {
		   //加载驱动到虚拟机
		Class.forName(driver);
		conn=DriverManager.getConnection(url,username,password);
	} catch (Exception e) {
		e.printStackTrace();
	}
	   //准备sql命令
	   String sql="delete from Pms_product_attribute_category where id=1";
	   Statement st=null;
	   try {
		st=conn.createStatement();
		int row=st.executeUpdate(sql);
		System.out.println(row>0?"删除成功":"删除失败");
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	   
   }
   @Test
   public void selet() {
	   Connection conn=null;
	   String username="root";
	   String password="root";
	   String driver="com.mysql.jdbc.Driver";
	   String url="jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8";
	   try {
		Class.forName(driver);
		conn=DriverManager.getConnection(url,username,password);
	} catch (Exception e) {
		e.printStackTrace();
	}
	   //准备sql命令
	   String sql="select * from Pms_product_attribute_category ";
	   Statement st=null;
	   ResultSet rs=null;
	   try {
		st=conn.createStatement();
		rs=st.executeQuery(sql);
		//处理结果集
		while (rs.next()) {
			//通过columnLabel列名或者别名获取,
			System.out.println(rs.getInt("id"));
			System.out.println(rs.getObject("name"));
			System.out.println(rs.getObject("attribute_count"));
			System.out.println(rs.getObject("param_count"));
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	   
   }

}
